// Learn TypeScript:
//  - https://docs.cocos.com/creator/manual/en/scripting/typescript.html
// Learn Attribute:
//  - https://docs.cocos.com/creator/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - https://docs.cocos.com/creator/manual/en/scripting/life-cycle-callbacks.html

import {
    tipMaxCount,
    simpleNum
} from "../utils/CommonUtils";

import {
    NotificationCenter
} from "../utils/NotificationCenter";

const {ccclass, property} = cc._decorator;

@ccclass
export default class TipLogic extends cc.Component {

    private stableDur: number = 0;
    private currDur: number = 0;
    private tipIndex: number = 0;

    // LIFE-CYCLE CALLBACKS:

    onLoad () {
        NotificationCenter.Instance.on('add_pop_tip', this.addOne, this);
    }
    
    onDestroy() {
        NotificationCenter.Instance.off('add_pop_tip', this.addOne, this);
    }

    start () {

    }

    update (dt) {
        if (this.currDur > 0) {
            this.currDur -= dt;
            if (this.currDur <= 0) {
                this.currDur = 0;
                let t = cc.tween;
                t(this.node)
                    .to(0.2, {opacity: 110})
                    .call(() => {
                        this.node.active = false;
                    })
                    .start();
            }
        }
    }

    public setupView(stableDur: number, type: number, iconRes: string, count: number, tipString: string){
        this.stableDur = stableDur;
        this.currDur = stableDur;
        this.tipIndex = 0;

        let im_icon = this.node.getChildByName('im_icon');
        let label1 = this.node.getChildByName('label1');
        let label_count = this.node.getChildByName('label_count');
        let label_tip = this.node.getChildByName('label_tip');
        if (type == 1) {
            label_count.getComponent(cc.Label).string = 'X '+simpleNum(count || 0);
            let iconWidth = im_icon.getContentSize().width;
            cc.loader.loadRes(iconRes, cc.SpriteFrame, (err, spriteFrame) => {
                im_icon.getComponent(cc.Sprite).spriteFrame = spriteFrame;
                im_icon.scale = iconWidth/im_icon.getContentSize().width;
                im_icon.scale = Math.min(1, im_icon.scale);
            });
            label_tip.active = false;
            label1.active = true;
            label_count.active = true;
            im_icon.active = true;
        }else{
            label_tip.getComponent(cc.Label).string = tipString;
            label_tip.active = true;
            label1.active = false;
            label_count.active = false;
            im_icon.active = false;
        }
    }

    public addOne() {
        this.tipIndex++;
        if (this.tipIndex>tipMaxCount) {
            this.node.active = false;
        }
        let t = cc.tween;
        let pos = cc.v2(this.node.position);
        pos.y += 130;
        t(this.node)
            .to(0.2, {position: cc.v3(pos)})
            .call(() => {
                this.currDur = this.stableDur;
            })
            .start();
    }
}
